import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import List from '../components/List.vue'
import Achievement from '../components/Achievement.vue'
import Student from '../components/Student.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "Login",
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      { path: "/list", component: List },
      { path: "/achievement", component: Achievement },
      { path: "/student", component: Student },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path !== "/") {
    if (localStorage.getItem("usr")) {
      next()
    } else {
      next("/")
    }
  } else {
    if (localStorage.getItem("usr")) {
      next("/list")
    } else {
      next()
    }
  }
})
export default router
